package com.example.demo;

import com.example.demo.data.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.Date;

@Controller
public class showResult {

    @RequestMapping(value = "/showResult")
    public String ShowResult(){
        return "showResult";
    }



    private static String savePath = "G:\\博士学习\\工作\\数据库\\实现\\new\\result\\result.txt";
    @RequestMapping("/getResult.do")
    @ResponseBody
    public static Result getResult(@RequestBody Result result) throws IOException {

        //读取文本文件
        File file = new File(savePath); // 创建File类对象

        FileReader fileReader = new FileReader(file);
        LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
        lineNumberReader.skip(Long.MAX_VALUE);
        int lines = lineNumberReader.getLineNumber() + 1;
        lineNumberReader.close();
        String ip[] = new String[lines];
        String sql[] = new String[lines];
        String time[] = new String[lines];



        BufferedReader br = null; // 创建reader缓冲区将文件流装进去
        try{
            int i=0;
            br = new BufferedReader(new FileReader(file));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                String[] temp = lineTxt.split("#");
                ip[i] = temp[0];
                sql[i] = temp[1];
                time[i] = temp[2];
                ++i;
            }
            br.close();
        }catch(Exception e){
            System.out.println(e);
        }finally {
            br.close();
        }

        result.setIp(ip);
        result.setSql(sql);
        result.setTime(time);
        return result;
    }
    @RequestMapping("/clearResult.do")
    @ResponseBody
    public static Result clearResult(@RequestBody Result result) throws IOException {
        FileWriter fw = null;
        try {
            File f=new File(savePath);
            fw = new FileWriter(f, false);

        }catch (Exception e){
            System.out.println(e);
        }

        fw.write("");
        try {
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}
